﻿using rehot;
using rehot.plugin;
using SanlieHash;
using SanlieHash.Fast;
using SanlieHash.GuoMi;
using SanlieHash.OtherHash;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var zuc = FastGuoMi.GetZUC("test", "hello");

            zuc.Jump(10000);
            for (var i = 0; i < 10; i++) Console.WriteLine(zuc.NextHex() + "   " + zuc.NowPos);

            var sm4 = new SM4();
            sm4.SetKey(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 });
            var sm4rlt = sm4.Encrypt(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 });
            Console.WriteLine(Helper.ToHexString(sm4rlt));
            var sm4rlt2 = sm4.Decrypt(sm4rlt);
            Console.WriteLine(Helper.ToHexString(sm4rlt2));

            var sm4a2 = FastGuoMi.EncryptSM4("test", "pwd");
            var sm4a3 = FastGuoMi.DecryptSM4(sm4a2, "pwd");
            Console.WriteLine(sm4a2); // G6vNRnnPYvBM86LCZ38qWQ==
            Console.WriteLine(sm4a3);

            FastHash.GetMD5("test");
            //var sm3a1 = FastHash.GetSM3("test");
            //Console.WriteLine(sm3a1);

            var d1 = FastAES.Encrypt2Base64("123", "123");
            Console.WriteLine(d1);
            Console.WriteLine(FastAES.Decrypt(FastEncode.Base64Decode2Bytes(d1), "123"));
            var a = FastEncode.Base64Encode("123");
            Console.WriteLine(a);
            Console.WriteLine(FastEncode.Base64Decode(a));

            var b = new CRC32_BZIP2();
            var c = b.Make(new byte[5] { 0, 1, 0, 2, 0 });
            var cc = BitConverter.ToUInt32(Helper.ToBytes(c));
            Console.WriteLine(cc.ToString());
            var s = Helper.ToHexString(new byte[5] { 0, 1, 0, 2, 0 });
            var xx = Helper.ToBytes(s);
            var x = SanlieHash.BlockCipher.AES.EncryptBytes("123", new byte[5] { 0, 1, 0, 2, 0 });
            Console.WriteLine(string.Join(' ', x.Select(i => i.ToString()).ToArray()));
            x = SanlieHash.BlockCipher.AES.DecryptBytes("123", x);
            Console.WriteLine(string.Join(' ', x.Select(i => i.ToString()).ToArray()));
            Console.WriteLine(x.Length);
            Console.WriteLine("Hello, World!");

            var ass = RehotAssembly.GetDllByAssembly(typeof(SM3).Assembly);
            foreach (var hash_id in ass.FindTypesIdByInterface("IHash"))
            {
                var hottype = ass.FindTypeById(hash_id);
                Test(hottype);
            }
        }

        internal static void Test(HotType hotType)
        {
            Console.Write(hotType.Name);
            var hash = hotType.New<IHash>();
            var a = "";
            var b = "";
            var c = "";
            var test_file = "SanlieHash.dll";
            {
                var len = 1000;
                var buf = new byte[len];
                using var file = File.OpenRead(test_file);
                hash.New();
                while (file.Position < file.Length - len)
                {
                    file.Read(buf, 0, len);
                    hash.Update(buf,len);
                }
                var size = (int)(file.Length - file.Position);
                if (size > 0)
                {
                    file.Read(buf, 0, size);
                    hash.Update(buf, size);
                }
                a = hash.Make();
            }
            {
                var buf = new byte[4096];
                using var file = File.OpenRead(test_file);
                hash.New();
                while (file.Position < file.Length - buf.Length)
                {
                    file.Read(buf, 0, buf.Length);
                    hash.Update(buf);
                }
                var size = (int)(file.Length - file.Position);
                if (size > 0)
                {
                    var __buf = new byte[size];
                    file.Read(__buf, 0, size);
                    hash.Update(__buf);
                }
                b = hash.Make();
            }
            {
                var buf = new byte[1000];
                var len = 999;
                using var file = File.OpenRead(test_file);
                hash.New();
                while (file.Position < file.Length - len)
                {
                    file.Read(buf, 1, len);
                    hash.Update(buf, 1, len);
                }
                var size = (int)(file.Length - file.Position);
                if (size > 0)
                {
                    file.Read(buf, 1, size);
                    hash.Update(buf, 1, size);
                }
                c = hash.Make();
            }
            Console.Write("   ");
            if (a == b && b == c && a.Length << 3 == hash.HashBits) Console.WriteLine(":  PASS");
            else Console.WriteLine(":   ERR");
        }
    }
}
